package ingredient

import (
	"context"

	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/svc"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/dberrorhandler"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/types/pancakebot"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetIngredientByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetIngredientByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIngredientByIdLogic {
	return &GetIngredientByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetIngredientByIdLogic) GetIngredientById(in *pancakebot.IDReq) (*pancakebot.IngredientInfo, error) {
	result, err := l.svcCtx.DB.Ingredient.Get(l.ctx, in.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	return &pancakebot.IngredientInfo{
		Id:          &result.ID,
		CreatedAt:    pointy.GetPointer(result.CreatedAt.UnixMilli()),
		UpdatedAt:    pointy.GetPointer(result.UpdatedAt.UnixMilli()),
		Name:	&result.Name,
		Unit:	&result.Unit,
		Value:	pointy.GetPointer(int64(result.Value)),
		ImageUrl:	&result.ImageURL,
	}, nil
}

